<?php
namespace app\back\model;


use app\common\model\CommentComm;


///留言表
class CommentModel extends CommentComm
{


    ///写入管理员回复
    public function insertManagerReplyData($repay_id,$repay_content){
        $this->insNewComment(['parent_com_id' => $repay_id, 'com_title' => '管理员回复','com_reply_status'=>1,'com_show_status'=>1,'com_ip'=>'127.0.0.1','com_content'=>$repay_content]);
    }



    //修改回复记录状态
    public function updateReplyStatus($repay_id){
        return $this->where('com_id', $repay_id)->update(['com_reply_status' => 1,'com_show_status'=>1,'update_time'=>date('Y-m-d H:i:s')]);
    }

    ///根据ID和当前数据状态修改状态
    public function updateStatusByIDAndStatus($repay_id,$old_show_status,$com_show_status){
        return $this->where('com_id', $repay_id)->where('com_show_status',$old_show_status)->update(['com_show_status' => $com_show_status]);
    }



    ///获取列表页数据
    public function getIndexDataList(){
        return $this->order('com_reply_status asc,com_show_status asc，com_id asc')->column('com_id,parent_com_id,com_title,com_email,com_content,com_reply_status,com_show_status,com_ip,create_time,update_time', 'com_id');
    }

    ///获取指定数据id留言
    public function getOneData($com_id = 0){
        return $this->where('com_id',$com_id)->field('com_id,parent_com_id,com_title,com_email,com_content,com_reply_status,com_show_status,com_ip,create_time,update_time')->find();
    }




    /// 用于存放子级数据
    private $child = [];
    ///获取一级评论数据
    public function getCommentData(){
        $top_map = [
            'parent_com_id' =>  0
        ];
        ///获取一级评论数据{desc最新的评论显示在顶部}
        $top_comment_data = $this->alias('b')->where($top_map)->order('b.com_show_status asc,b.com_reply_status asc,b.update_time desc')->column('b.com_id,b.parent_com_id,b.com_title,b.com_email,b.com_content,b.com_reply_status,b.com_show_status,b.com_ip,b.create_time,b.update_time', 'b.com_id');


        foreach ($top_comment_data as $k => $v) {
            $top_comment_data[$k]['show_parent_id'] = -1;
            $top_comment_data[$k]['com_content'] = htmlspecialchars_decode($v['com_content']);
            // 获取二级评论
            $this->child = [];
            $this->getTree($v,$v['com_id']);
            $child_data = $this->child;

            ///修正排序，按照评论id降序{小到大}
            if (!empty($child_data)) {
                // 按评论时间asc排序
                uasort($child_data, function ($a, $b) {
                    $prev = $a['com_id'] ?? 0;
                    $next = $b['com_id'] ?? 0;
                    if ($prev == $next) {
                        return 0;
                    }

                    return ($prev < $next) ? -1 : 1;
                });
            }
            $top_comment_data[$k]['son_num'] = count($child_data);
            $top_comment_data[$k]['child'] = $child_data;
        }

        return $top_comment_data;
    }
    // 递归获取子级评论树状结构
    private function getTree($data,$show_parent_id)
    {
        $son_map = [
            'parent_com_id' =>  $data['com_id'] ?? -1
        ];
        ///获取当前评论的子级数据
        $child_data = $this->where($son_map)->order('com_id desc')->column('com_id,parent_com_id,com_title,com_email,com_content,com_reply_status,com_show_status,com_ip,create_time,update_time', 'com_id');

        if (!empty($child_data)) {
            foreach ($child_data as $k => $v) {
                $v['show_parent_id'] = $show_parent_id;
                $v['com_content']  = htmlspecialchars_decode($v['com_content']);
                $this->child[] = $v;
                $this->getTree($v,$show_parent_id);
            }
        }
    }



}